//
//  AppDelegate.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/8.
//  Copyright © 2015年 姚彦兆. All rights reserved.
/*

┏ ┛ ┻━━━┛ ┻ ┓
┃｜｜｜｜｜｜｜┃
┃　　 　━　　　┃
┃　 ┳┛ 　┗┳  ┃
┃　　　　　　　┃
┃　　 　┻　 　┃
┃　　　　 　　┃
┗━┓　　　  ┏━┛
　　┃　史　┃
　　┃　诗　┃
　　┃　之　┃
　　┃　宠　┃
　　┃　　　┗ ━ ━ ━┓
　　┃经验与我同在　┣┓
　　┃攻楼专用宠物　┃
　　┗┓ ┓ ┏━┳ ┓ ┏ ┛
　　　 ┃ ┫┫ ┃┫ ┫
　　　 ┗┻┛　 ┗┻┛


*/

/*

     ∧_∧::
　 (´･ω･`)::
  /⌒　　⌒)::
 /へ_＿  / /::
(＿＼＼  ﾐ)/::
　 ｜ `-イ::
　 /ｙ　 )::
　/／  ／::
／　／::
(　く:::
|＼ ヽ:::

*/

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //测试沙盒
//        let userAccountInfo = UserAccountModel.getUserInfoFromDocumentDirectoryPlist()

//        print(userAccountInfo)
        
        //程序开始,Launch界面加载完毕之后,设置自定义的控制器
        
        //1.设置主窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
        //2.为主窗口设置控制器
//        window?.rootViewController = MainTabBarController ()
        
//        window?.rootViewController = CSYComposePageViewController()
        
        window?.rootViewController = appShouldLoadController()
        
//        window?.rootViewController = WelcomePageViewController ()
        
        //3.设置控制器的主体颜色
        setThemeColor()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeViewController:", name: APPDELEGATESHOULDSWITCHROOTVIEWCONTROLLER, object: nil)
        
        return true
    }
    
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
    /**
    跳转控制器的方法
    */
    @objc private func changeViewController(notice : NSNotification){
    
        if (notice.object as? String) != nil{
        
            window?.rootViewController = WelcomePageViewController()
            
            return
            
        }
        
        window?.rootViewController = MainTabBarController()
    
    }
    
    /**
     设置选择控制器的方法
     */
    private func appShouldLoadController() -> UIViewController{
    
        if GetUserInfoModel().isLogin {
            
            return isNewVersion() ? NewfeatureCollectionViewController() : WelcomePageViewController()

        }

        return MainTabBarController()
        
    }
    
    
    /**
     比较新旧版本
     */
    
    private func isNewVersion () -> Bool{
    
        //取得新版本
        let newVersionSTR = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! NSString
        
        let newVersion = newVersionSTR.doubleValue
        
        //获得老版本

        let userDefaults = NSUserDefaults.standardUserDefaults()
        
        let oldversion = userDefaults.doubleForKey(versionKey)
        
        //保存新版本
        
        userDefaults.setDouble(newVersion, forKey: versionKey)
        
        userDefaults.synchronize()
        
        //检查版本更新
        
        return newVersion > oldversion
    
    }

    func setThemeColor () {
    
        UINavigationBar.appearance().tintColor = themeColor
        
        UITabBar.appearance().tintColor = themeColor
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


    
    
}


